package com.youkeda.dewu.api;

import com.youkeda.dewu.model.Paging;
import com.youkeda.dewu.model.Product;
import com.youkeda.dewu.model.Result;
import com.youkeda.dewu.param.BasePageParam;
import com.youkeda.dewu.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName ProductApi.java
 * @Description TODO
 * @createTime 2020年10月07日 20:27:00
 */
@RestController
@RequestMapping("/api/product")
public class ProductApi {
    @Autowired
    private ProductService productService;

    @GetMapping ("/page")
    public Result<Paging<Product>> page( BasePageParam pageParam){
        Result<Paging<Product>> result = new Result<>();
        result.setSuccess(true);
        if (pageParam == null){
            result.setSuccess(false);
            return result;
        }
        Paging<Product> productPaging = productService.pageQueryProduct(pageParam);
        if (productPaging ==null){
            result.setSuccess(false);
            return  result;
        }
        result.setData(productPaging);
        result.setMessage("success");
        return result;
    }

    @GetMapping("/get")
    public Result<Product> get(String productId){
        Result<Product> result = new Result<>();
        result.setSuccess(true);
        if (StringUtils.isEmpty(productId)){
            result.setSuccess(false);
            return result;
        }
        Product product = productService.get(productId);
       result.setData(product);
       return result;

    }
}
